package com.chilli.cosima.action.providermanager;

import java.io.File;

import org.apache.struts2.ServletActionContext;

import com.chilli.cosima.action.BaseSupport;
import com.chilli.cosima.common.BussinessException;
import com.chilli.cosima.data.bean.Provider;
import com.chilli.cosima.service.ProviderService;

public class ProviderDelAction extends BaseSupport
{

	private static final long serialVersionUID = -911350709205731016L;

	private int id;
	private ProviderService providerService;

	public String execute()
	{
		try
		{
			Integer[] ids = new Integer[]
			{ id };
			Provider provider = providerService.findProviderById(id);
			providerService.deleteProvider(ids);
			if (null != provider.getPicture()
					&& !"".equals(provider.getPicture()))
			{
				File imageFile = new File(ServletActionContext
						.getServletContext().getRealPath(
								"/upload/images/provider")
						+ "/" + provider.getPicture());
				if (imageFile.exists())
				{
					imageFile.delete();
				}
			}
		} catch (BussinessException e)
		{
			logger.error("error", e);
		}

		return SUCCESS;
	}

	public int getId()
	{
		return id;
	}

	public void setId(int id)
	{
		this.id = id;
	}

	public ProviderService getProviderService()
	{
		return providerService;
	}

	public void setProviderService(ProviderService providerService)
	{
		this.providerService = providerService;
	}

}
